//
//  SZLSellerMyBillViewController.m
//  Shengzhuli
//
//  Created by subook on 16/8/11.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLSellerMyBillViewController.h"
#import "SellerMyBillTableViewCell.h"
#import <AFNetworking/AFNetworking.h>
#import <MJRefresh.h>
#import "SZLAccountHelper.h"
#import "SuModelTool.h"
#import "SellerSaleDetailModel.h"
#import "BusinessNetwork.h"
#import "MBProgressHUD+MJ.h"

@interface SZLSellerMyBillViewController () <UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _pageNo;
}
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,assign) float sumAmount;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,weak) UILabel *moneyLabel;

@end

@implementation SZLSellerMyBillViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"销售明细";
    _pageNo = 1;
    [self downloadData];
}
- (void)downloadData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"token"] = [SZLAccountHelper shareInstance].token;
    params[@"pageNo"] = @(_pageNo);
    [BusinessNetwork getSaleDetailListWithParame:params response:^(BOOL success, NSString *msg, NSArray *lists,float sumAmount) {
        if (success) {
            self.sumAmount = sumAmount;
            if (lists == 0) {
                return ;
            }
            if (lists.count == 0&&_pageNo>1) {
                _pageNo--;
                return;
            }
            self.tableView.mj_footer.hidden = (lists.count<20)?YES:NO;
            [self.dataArray addObjectsFromArray:lists];
            [self setTableView];
            [self.tableView.mj_footer endRefreshing];
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)setTableView
{
    [self.view addSubview:self.tableView];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"销售详情";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SellerMyBillTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SellerMyBillTableViewCell class])];
    cell.model = self.dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, ScreenW, 120))];
        
        UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_img_my_bill"]];
        image.frame = _bgView.frame;
        [_bgView addSubview:image];
        
        UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(10, 10, 110, 30))];
        label.text = @"销售额(元):";
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:14];
        [_bgView addSubview:label];
        
        UILabel *moneyLabel = [[UILabel alloc] initWithFrame:(CGRectMake(ScreenW/2-50, 60, 100, 30))];
        moneyLabel.textAlignment = NSTextAlignmentCenter;
        moneyLabel.textColor = [UIColor whiteColor];
        self.moneyLabel = moneyLabel;
        moneyLabel.text = [NSString stringWithFormat:@"%.2f",self.sumAmount];
        [_bgView addSubview:moneyLabel];
    }
    return _bgView;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, ScreenW, ScreenH-64))];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.bgView;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.rowHeight = 135;
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _pageNo++;
            [self downloadData];
        }];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SellerMyBillTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([SellerMyBillTableViewCell class])];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
